package com.cpi.cornpp.version.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.Globals;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;
import org.apache.struts.util.MessageResources;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.exchangerateversion.exception.ExchangeRateVersionException;
import com.cpi.cornpp.exchangerateversion.service.ExchangeRateVersionService;
import com.cpi.cornpp.version.form.VersionForm;

public class AdjustValuesToVersionCurrencyAction extends MappingDispatchAction{

	private final static String DEFAULT = "default";
	/** Custom exception */
	private final static String CUSTOM_EXCEPTION = "custom_exception";//$NON-NLS-1$	
	/** Edit add key */
	private static final String SUCCESS_EDIT_LABEL = "common.edit.success";//$NON-NLS-1$
	/** Information message name*/
	private final static String MESSAGE = "MESSAGE";//$NON-NLS-1$
	
	public ActionForward adjustValuesToVersionCurrency(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		ExchangeRateVersionService service = null;
		List listERVViews = null;
		List listERVGroups = null;
		VersionForm versionForm = null;
		HttpSession httpSession = null;
		/*int orderField = 0;
		int orderType = 0;*/
		try{
			listERVViews = new ArrayList(0);
			service = new ExchangeRateVersionService();
			
			versionForm = (VersionForm) form;
			httpSession = request.getSession(false);
			/*if(form != null){
				orderField 	= versionForm.getOrderField();
				orderType 	= versionForm.getOrderType();
			}*/
			listERVViews = service.getExchangeRateVersion(versionForm.getVersionView().getVersionId());
			listERVGroups = service.getGroups(versionForm.getVersionView().getVersionId());
			request.setAttribute("listERVViews", listERVViews);
			versionForm.setExchangeRateGroups(listERVGroups);			
		}catch (ExchangeRateVersionException versionException){
			httpSession.setAttribute(CUSTOM_EXCEPTION, versionException);
			throw versionException;
		}
		catch (Exception e){
			ExchangeRateVersionException versionException=null;
			e.printStackTrace(System.out);
			versionException= new ExchangeRateVersionException(e , CornPPException.LAYER_ACTION,CornPPException.ACTION_LISTS);
			httpSession.setAttribute(CUSTOM_EXCEPTION, versionException);
			throw versionException;
		} /*finally {
			httpSession.setAttribute("previousOrderField", Integer.toString(versionForm.getOrderField()));
			httpSession.setAttribute("previousOrderType", Integer.toString(versionForm.getOrderType()));
			httpSession.removeAttribute("versionId");
		}*/
		return mapping.findForward(DEFAULT);
	}
	
	/**
	 * Actualizar los valores 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward adjustValuesToVersionCurrencyUpdate(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		String message = null;
		MessageResources messageResources;
		VersionForm versionForm = null;
		versionForm = (VersionForm)form;
		HttpSession httpSession = null;
		ExchangeRateVersionService service = null;
		try {
			messageResources = getResources(request);
			service = new ExchangeRateVersionService();
			service.updateExchangeRate(versionForm.getExchangeRateGroups());
			httpSession = request.getSession(false);
			message = messageResources.getMessage((Locale)httpSession.getAttribute(Globals.LOCALE_KEY), SUCCESS_EDIT_LABEL);
			httpSession.setAttribute(MESSAGE, message);
		} catch (ExchangeRateVersionException versionException){
			httpSession = request.getSession(false);
			httpSession.setAttribute(CUSTOM_EXCEPTION, versionException);
			throw versionException;
		}
		catch (Exception e){
			ExchangeRateVersionException versionException=null;
			e.printStackTrace(System.out);
			versionException= new ExchangeRateVersionException(e , CornPPException.LAYER_ACTION,CornPPException.ACTION_LISTS);
			httpSession.setAttribute(CUSTOM_EXCEPTION, versionException);
			throw versionException;
		} 
		return mapping.findForward(DEFAULT);
	}
	//
}
